$(function() {
	var contextPath = $("#contextPath").val();
	var addressList=$("#addressList");
	var daddressId=$("#addressId").val();
	var load = layer.open({type: 2,shadeClose:false});
	dataInit();

function dataInit(){
	addressList.empty();
	$.ajax({
		url:contextPath+"/address/addressList",
		type:"post",
		async:true,
		dataType:"json",
		success : function(data, textStatus) {
			var state = data.result.state;
			html='';
			if(state==1){
				$.each(data.addressList,function(i,v){
					if(v.status==1){
						html+='<li class="address_select_item address_select_active" addressId='+v.id+'>';
					}else{
						html+='<li class="address_select_item" addressId='+v.id+'>';
					}
					var countryName ='';
					if(v.countryName ==null){
						countryName ='';
					}else{
						countryName = v.countryName
					}
					html+='<div class="address_select_top line__bottom_efeff4 fix"><span class="f0 dt r"></span></div><div class="address_select"><p class="address_info"><span class="name">'+v.receiverName+'</span><span class="phone">'+v.receiverPhone+'</span></p><p class="address_info"><i class="icon_address_01 dib vm"></i><span class="address_info_txt">'+v.provinceName+''+v.cityName+''+countryName+''+v.receiverAddress+'</span></p></div><div class="tr"><i class="icon_address_active"></i>';
					html+='</div></li>';
				});
				addressList.append(html);
			}
			$("#addressDiv").show();
			setTimeout(function(){layer.close(load);},50);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	});		
}

$("#addressList").on("click",".address_select_item",function(){
	$("#addressId").val($(this).attr("addressId"));
	var type = $("#type").val();
	if(type=='buy_now'){
		$("#addressForm").attr("action",contextPath+"/pay/buy_now").submit();
	}else{
		$("#addressForm").attr("action",contextPath+"/pay/confirm_shopCart").submit();
	}
	
});


});
